package com.fairytask.ui.load
{
	import com.fairytask.engine.GameStage;
	import com.jzonl.tools.ProcessTools;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.events.Event;
	import flash.events.TextEvent;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * 预加载页面 
	 * @author hanjy
	 * 
	 */	
	public class PreLoadUI extends FrameUI
	{
		private static var _instance:PreLoadUI;
		public var processBar	:ProcessTools;
		
		private var totalWidth	:int	=	650; //最大宽度
		public var currentProcess:Number;
		private var currentWidth:Number;
		
		//刷新提示文本
		private var _freshTxt			:TextField;
		private var _loadingStr:String;
		
		public function PreLoadUI(popUI:Boolean=false)
		{
			super(popUI);
		}

		public static function get instance():PreLoadUI
		{
			if(null==_instance)
			{
				_instance	=	new PreLoadUI();
			}
			return _instance;
		}
		
		/**
		 * 显示初始化
		 */
		override public function initGUI():void
		{
			initText();
			
			resetBg();
			
			processBar	=	ProcessTools.instance;
			addChild(processBar);
			processBar.x	=	(GameStage.rootW-processBar.width)/2;
			processBar.y	=	(GameStage.rootH-processBar.height)/2;
			
			//添加事件
			addEventListener(Event.RESIZE,resetBg);
		}
		
		/**
		 * 重设背景 
		 * @param evt
		 * 
		 */
		private function resetBg(evt:Event=null):void
		{
			//开始画背景
			graphics.beginFill(0);
			graphics.drawRect(GameStage.x,GameStage.y,GameStage.stageWidth,GameStage.stageHeight);
			graphics.endFill();
		}
		
		private function initText():void
		{
			var tf:TextFormat = new TextFormat();
			tf.font	=	"微软雅黑";
			tf.size = 14;
			_freshTxt = new TextField();
			_freshTxt.defaultTextFormat = tf;
			_freshTxt.selectable = false;
			_freshTxt.textColor = 0xffffff;
			_freshTxt.width = 250;
			_freshTxt.autoSize = TextFieldAutoSize.LEFT;
			_freshTxt.htmlText = "若当前加载速度过慢，请点击 <a href='event:fresh'><u><font color='#00ff00' textDecoration='underline'>刷新页面</u></a>";
			_freshTxt.x = (GameStage.rootW - _freshTxt.textWidth)/2;
			_freshTxt.y = 520;
			_freshTxt.addEventListener(TextEvent.LINK,onFresh);
			
			addChild(_freshTxt);
		}
		
		private function onFresh(evt:TextEvent):void
		{
			//调用js完成刷新
			ExternalInterface.call("refresh");
		}
		
		/**
		 * 更新进度条 
		 * @param val 是个比例
		 * 
		 */
		public function updateProcess(val:Number):void
		{
			currentProcess	=	val;
			processBar.process	=	currentProcess;
		}
		
		override public function hide(e:*=null):void
		{
			super.hide();
		}
		
		override protected function AfterHide():void
		{
			processBar.process	=	0;
		}
		
		override public function show(space:String=LOADLAYER, onshow:Function=null):void
		{
			super.show(space);
		}
		
		override protected function stageReSize(evt:Event=null):void
		{
			//绘制防点击
			graphics.clear();
			graphics.beginFill(0,0);
			graphics.drawRect(-GameStage.x,-GameStage.y,GameStage.stageWidth,GameStage.stageHeight);
			graphics.endFill();
		}
	}
}